/*
*	ID : Pallab
* 	PROG : E
* 	LANG : C++
* 	2012-12-13-18.16.04 Thursday
*
* 	"I have not failed, I have just found 10000 ways that won't work.
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <numeric>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>
#include <chrono>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <random>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define foR(i1,st,ed) for(int i1 = st , j1 = ed ; i1 < j1 ; ++i1 )
#define fo(i1,ed) foR( i1 , 0 , ed )
#define foE(i1,st,ed) foR( i1, st, ed+1 )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")

inline void wait(double seconds) {
    double endtime = clock() + (seconds * CLOCKS_PER_SEC);
    while (clock() < endtime) {
        ;
    }
}
template<class T>
inline T fastIn() {
    register char c=0;
    register T a=0;
    bool neg=false;
    while(c<33)c=getchar();
    while(c>33) {
        if(c=='-') {
            neg=true;
        } else {
            a= (a*10)+(c-'0');
        }
        c=getchar();
    }
    return neg?-a:a;
}


const int MX=50005;
struct node {
    Int t,s,p;
    int i;
};
inline bool cmp(node a, node b) {
    return a.t<b.t||(a.t==b.t&&a.i<b.i);
}

node task[MX];
int task_count,task_id;
Int T;
inline void read() {
    task_count=fastIn<int>();
    fo(i,task_count) {
        task[i].t=fastIn<Int>(),task[i].s=fastIn<Int>(),task[i].p=fastIn<Int>(),task[i].i=i;
    }
    T=fastIn<Int>();
}
Int finish_time[MX];
inline Int simulate(Int cur_priority) {
    task[task_id].p=cur_priority;

    multimap<Int,node > pq;
    Int cur_time=0;
    for(int i=0; i<task_count; ++i) {
        while( pq.empty()==false && cur_time<task[i].t ) {
            node tmp=pq.begin()->second;
            pq.erase( pq.begin() );
            if( cur_time+tmp.s<=task[i].t ) {
                //ae job'ta ses
                cur_time = cur_time+tmp.s;
                finish_time[ tmp.i ]=cur_time;
            } else {
                tmp.s = tmp.s-(task[i].t-cur_time);
                cur_time= task[i].t;
                pq.insert( mk( -abs(tmp.p), tmp) );
            }
        }
        pq.insert( mk( -abs(task[i].p), task[i]) );
        cur_time=task[i].t;
    }
    while(pq.empty()==false) {
        node tmp=pq.begin()->second;
        pq.erase( pq.begin() );
        cur_time=cur_time+tmp.s;
        finish_time[ tmp.i ]=cur_time;
    }

    return finish_time[ task[task_id].i ];
}
Int test_priorities[MX];
Int given_priorities[MX];

inline void proc() {
    sort(task,task+task_count,cmp);
    fo(i,task_count) {
        if(task[i].p==-1) {
            task_id=i;
            break;
        }
    }

    fo(i,task_count)given_priorities[i]=max(0LL,task[i].p);

    sort(given_priorities,given_priorities+task_count,less<int>() );
    int cnt=task_count,cc=0;
    given_priorities[cnt++]=given_priorities[task_count-1]+9;
    fo(i,cnt-1){
        if( given_priorities[i+1]-given_priorities[i]>=2 ){
            test_priorities[cc++]=given_priorities[i]+1;
        }
    }
//    fo(i,task_count)cout<<test_priorities[i]<<' ';
//    line;

    int lo=0,hi=cc;
    while(lo<=hi) {
        int mid=(lo+hi)>>1;
        Int pivot_value=simulate(test_priorities[mid]);
        if( pivot_value<T ) {
            hi=mid-1;
        } else if( pivot_value>T ) {
            lo=mid+1;
        } else {
            cout<<test_priorities[mid]<<'\n';
            cout<<finish_time[0];
            foR(i,1,task_count){
                cout<<' '<<finish_time[i];
            }
            line;
            return ;
        }
    }
}
int main() {
    int kase = 1;
#if defined( xerxes_pc )
    if (!freopen("in1", "r", stdin))
        puts("error opening file in "), assert(0);
    kase = 1;
#else
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    fo(i,kase) {
        read();
        proc();
    }
    return 0;
}
